package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whmskj.xjlhsc.jitapi.dto.Emp;
import com.whmskj.xjlhsc.mapper.Yg01employeefilesMapper;
import com.whmskj.xjlhbc.domain.Yg01employeefiles;
import com.whmskj.xjlhbc.service.Yg01employeefilesService;
import com.whmskj.xjlhbc.service.dto.EmpDto;
import com.whmskj.xjlhsc.utils.SCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author lk
 * @since 2025-07-16 11:35:41
 */
@Service
@Slf4j
public class Yg01employeefilesServiceImpl extends ServiceImpl<Yg01employeefilesMapper, Yg01employeefiles> implements Yg01employeefilesService {

    @Autowired
    private Yg01employeefilesMapper yg01employeefilesMapper;

    @Override
    public Page<Yg01employeefiles> findPage(Yg01employeefiles params) {
        Page<Yg01employeefiles> page = new Page<>(1, 10);//TODO 自行处理
        LambdaQueryWrapper<Yg01employeefiles> query = Wrappers.lambdaQuery(Yg01employeefiles.class);
        return yg01employeefilesMapper.selectPage(page, query);
    }

    @Override
    public EmpDto findList(Yg01employeefiles params) {
        LambdaQueryWrapper<Yg01employeefiles> query = Wrappers.lambdaQuery(Yg01employeefiles.class);
        if (StrUtil.isNotBlank(params.getKeyword())) {
            query.like(Yg01employeefiles::getName, params.getKeyword()).or().like(Yg01employeefiles::getIdNumber, params.getKeyword()).or().like(Yg01employeefiles::getContactCall, params.getKeyword()).or().like(Yg01employeefiles::getFileNumber, params.getKeyword());
        }
        List<Yg01employeefiles> list = yg01employeefilesMapper.selectList(query);
        EmpDto empDto = new EmpDto();
        if (CollectionUtil.isNotEmpty(list)) {
            try {
                empDto.setManCount(((int) list.stream().filter(cur -> "男".equals(cur.getGender())).count()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            empDto.setWomanCount(((int) list.stream().filter(cur -> "女".equals(cur.getGender())).count()));
            empDto.setTotalCount(list.size());
        }
        empDto.setEmpList(list);
        return empDto;
    }

    @Override
    public Yg01employeefiles findById(Long id) {
        return yg01employeefilesMapper.selectById(id);
    }

    @Override
    public boolean insert(Yg01employeefiles yg01employeefiles) {
        return save(yg01employeefiles);
    }

    @Override
    public boolean update(Yg01employeefiles yg01employeefiles) {
        return updateById(yg01employeefiles);
    }

    @Override
    public int delete(Long id) {
        return yg01employeefilesMapper.deleteById(id);
    }

    /**
     * 获取可以奖惩的员工列表
     * @return
     */
    @Override
    public List<Yg01employeefiles> getEmpList(String name) {
        Emp emp = SCUtil.getCurrentLoginEmp();
        List<Yg01employeefiles> list=yg01employeefilesMapper.selectEmpList(emp.getEmpName(),name);
        return list;
    }

    @Override
    public List<String> selectProductionOfStaff(String userId) {
       return yg01employeefilesMapper.selectProductionOfStaff(userId);
    }

    /**
     * 调整员工工种
     * @param yg01employeefiles
     */
    @Override
    public void yg01employeefilesService(Yg01employeefiles yg01employeefiles) {
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        Assert.isTrue("统计".equals(currentLoginEmp.getTypeOfWork()),"没有权限");
        yg01employeefilesMapper.updateById(yg01employeefiles);

    }

}